/**
 * 
 */
package hu.cubussapiens.facts.query;

import hu.cubussapiens.cms.PMF;
import hu.cubussapiens.facts.AccessTicket;
import hu.cubussapiens.facts.BlobData;
import hu.cubussapiens.facts.StringData;

import java.io.PrintWriter;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * @author balage
 *
 */
public class QueryContext implements IQueryContext{
	
	private PersistenceManager pm;
	
	private PrintWriter writer;
	
	public QueryContext(PrintWriter writer) {
		pm = PMF.get().getPersistenceManager();
		this.writer = writer;
	}

	@Override
	public void debug(String data) {
		writer.println("DEBUG: "+data);
	}
	
	@Override
	public PersistenceManager getManager() {
		return pm;
	}

	public void dispose(){
		pm.close();
		pm = null;
	}
	
	private AccessTicket getTicket(Long id){
		javax.jdo.Query query = pm.newQuery(AccessTicket.class);
		query.setFilter("id == "+id);
		List<?> result = (List<?>)query.execute();
		for(Object o : result){
			if (o instanceof AccessTicket){
				//writer.println("AT: "+((AccessTicket) o).id);
				if (id.equals(((AccessTicket) o).getId()))
					return (AccessTicket)o;
			}
		}
		return null;
	}
	
	//
	
	@Override
	public boolean canAccess(Long accessTicket) {
		if (accessTicket.longValue() == -1l)
			return true;
		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        
        AccessTicket at = getTicket(accessTicket);//pm.getObjectById(AccessTicket.class, accessTicket);
        if (at == null){
        	//writer.println("Access ticket not found: "+accessTicket);
        	return false;
        }
        
        if (at.getUsers() == null)
        	throw new NullPointerException("Access ticket error: "+at.getId()+" "+at.getOwner()+" "+at.getUsers());
        
        //writer.println("#"+at.getUsers().size());
        for(String emil : at.getUsers()){
        	//writer.println("Access cheking: "+emil);
        	if (emil.trim().equals(user.getEmail().trim()))
        		return true;
        }
        
		return false;
	}
	
	public BlobData getBlobData(Long ID){
		javax.jdo.Query query = pm.newQuery(BlobData.class);
		query.setFilter("ref == "+ID);
		List<?> result = (List<?>)query.execute();
		for (Object o : result){
			if (o instanceof BlobData){
				return ((BlobData) o);
			}
		}
		return null;
	}
	
	@Override
	public StringData getLiteral(String literal) {
		javax.jdo.Query query = pm.newQuery(StringData.class);
		query.setFilter("data == '"+literal+"'");
		List<?> result = (List<?>)query.execute();
		for (Object o : result){
			if (o instanceof StringData){
				return ((StringData) o);
			}
		}
		return null;
	}

	public StringData getData(Long ID) {
		javax.jdo.Query query = pm.newQuery(StringData.class);
		query.setFilter("id == "+ID);
		List<?> result = (List<?>)query.execute();
		for (Object o : result){
			if (o instanceof StringData){
				return ((StringData) o);
			}
		}
		return null;
	}
	
}
